import 'dart:math';

import 'package:my_soft/pages/home/logic.dart';
import 'package:my_soft/pages/home/search_field.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class BuildSeachbar extends StatelessWidget {
  BuildSeachbar({super.key});
  final logic = Get.put(HomeLogic());
  @override
  Widget build(BuildContext context) {
    return SliverPersistentHeader(
      // 锁定
      pinned: true,
      // 浮动
      // floating: true,
      delegate: MySliverSearchBarDelegate(
        minHeight: 42,
        maxHeight: 52,
        child: SearchField(
          hintText: '搜索框',
          searchController: logic.searchCtrl,
          searchfocusNode: logic.textFocus1,
          onEditingComplete: logic.searchcompleter,
        ),
      ),
    );
  }
}

class MySliverSearchBarDelegate extends SliverPersistentHeaderDelegate {
  final Widget child;
  final double minHeight; // 最大高度
  final double maxHeight; // 最小高度

  MySliverSearchBarDelegate({
    required this.child,
    required this.minHeight, // 最大高度
    required this.maxHeight,
  });

  @override
  double get maxExtent => max(maxHeight, minHeight);

  @override
  double get minExtent => minHeight;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    // return child;
    // return SizedBox.expand(child: child);
    return SizedBox.expand(
      child: Container(
        decoration: const BoxDecoration(color: Colors.white),
        child: child,
      ),
    );
  }

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    // return true;
    return false;
  }
}
